<?php

namespace addons\classroom\controller\api;

use app\common\controller\Frontend;
use think\Db;


class Auth extends Frontend
{

    protected $noNeedLogin = ['*'];

    public function captcha()
    {
        // header("Access-Control-Allow-Origin: *");
        $cors_request_domain = explode(",",config('fastadmin.cors_request_domain'));
        $hostname = $_SERVER['HTTP_HOST'];
        $requestOrigin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : $hostname;
        foreach ($cors_request_domain as $k=>$v) {
            $tmp = str_replace("http://","",$v);
            $tmp = str_replace("https://","",$tmp);
            if($hostname == $tmp){
                header("Access-Control-Allow-Origin: {$requestOrigin}");
            }
        }

        $captcha = '';
        for ($i = 0; $i < 4; $i++) {
            switch (mt_rand(0, 2)) {
                case 0: // 数字
                    $captcha .= chr(mt_rand(49, 57));
                    break;
                case 1: // 大写字母
                    $captcha .= chr(mt_rand(65, 90));
                    break;
                case 2: // 小写字母
                    $captcha .= chr(mt_rand(97, 122));
                    break;
            }
        }

        // 创建画布
        $img = imagecreatetruecolor(122, 30);
        // 给背景分配颜色
        $bg = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
        // 填充背景色
        imagefill($img, 0, 0, $bg);

        // 设置字体大小和颜色
        $font_size = 20; // 这里设置你想要的文字大小
        $font_color = imagecolorallocate($img, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150));

        // 确保你的服务器上有这个字体文件
        $font_file = APP_PATH . '/../vendor/topthink/think-captcha/assets/ttfs/4.ttf'; // 指定字体文件路径

        // 循环写入文字
        for ($i = 0; $i < 4; $i++) {
            // 计算文字位置
            $x = 12 + $i * ($font_size / 0.8); // 调整X坐标以适应不同字号
            $y = ($font_size / 2) + 12; // 调整Y坐标以居中文字

            imagettftext($img, $font_size, 0, $x, $y, $font_color, $font_file, $captcha[$i]);
        }

        // 将图像转换为二进制数据
        ob_start();
        imagepng($img);
        $binary_data = ob_get_contents();
        ob_end_clean();

        // 释放资源
        imagedestroy($img);

        // 将二进制数据转换为Base64编码
        $base64_image = base64_encode($binary_data);

        // 返回Base64编码的图像字符串
        $captchaId = $this->uni_rand(8);
        $time = time();
        Db::name("classroom_code")->insert(['id'=>$captchaId,'code'=>$captcha,'createtime'=>$time]);
        $old_time = $time - 60;
        Db::name("classroom_code")->where("createtime < $old_time")->delete();
        return json(["code"=>1,"captchaId"=>$captchaId,'picPath'=>"data:image/png;base64,{$base64_image}"]);
    }

    function uni_rand($len)
    {
        $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $string=substr(time(),-3);
        for(;$len>=1;$len--)
        {
            $position=mt_rand()%strlen($chars);
            $position2=mt_rand()%strlen($string);
            $string=substr_replace($string,substr($chars,$position,1),$position2,0);
        }
        return $string;
    }
}
